layui.use(["element", 'layer', 'form'], function () {
  var element = layui.element,
    form = layui.form,
    layer = layui.layer;
});
// AssetCode  资产代码  

var vm = new Vue({
  el: '#app',
  data: {
    ArrData: [{
        AssetClass: '1',
        amount: '200,000',
        proportion: '20%',
        price: '5.5%',
        mark: '',
        clasN:'非标准化债权类资产'
      },
      {
        AssetClass: '2',
        amount: '250,000',
        proportion: '25%',
        price: '5%',
        mark: '',
        clasN:'债券'
      },
      {
        AssetClass: '3',
        amount: '400,000',
        proportion: '40%',
        price: '4.5%',
        mark: '',
        clasN:'同业存款'
      },
      {
        AssetClass: '4',
        amount: '100,000',
        proportion: '10%',
        price: '6%',
        mark: '',
        clasN:'同业存单'
      },
      {
        AssetClass: '5',
        amount: '50,000',
        proportion: '10%',
        price: '4%',
        mark: '',
        clasN:'回购'
      }
    ],
    totalVisible: false,
    total: 1000000,
    syl_txt: "组合收益率：4.475%",
    spzje_txt:'150,000'
  },
  mounted: function () {
    this.init()
  },
  methods: {
    init: function () {
      localStorage.setItem('navigation', '<a href="#">首页</a><span lay-separator="">/</span><a href="">大类资产配置模型</a><span lay-separator="">/</span><a><cite>大类资产配置清单</cite></a>');
      this.setPrice();
    },
    del: function (index) {
      this.ArrData.splice(index, 1);
      this.setPrice();
    },
    setPortion: function (index) {
      var obj = this.ArrData[index];
      var am = Number(obj.amount.replace(/,/g, ''));
      if(obj.clasN=='非标准化债权类资产' && am>420000){
        layer.alert('非标准化债权类资产配置金额占比<br/>不得超过420,000万元。', {
          icon: 2,
          btnAlign: 'c',
          shade: false,
          end:function(){
            vm.ArrData[index].amount ='200,000';
          }
        });
        return;
      }
      // if(obj.clasN=='债券' && am>600000){
      //   layer.alert('债券类型资产配置比例不得超过30%', {
      //     icon: 2,
      //     btnAlign: 'c',
      //     shade: false,
      //     end:function(){
      //       vm.ArrData[index].amount ='100,000';
      //     }
      //   });
      //   return;
      // }
      // if(obj.clasN=='同业存单' && am>300000){
      //   layer.alert('同业存单类型资产配置比例不得超过30%', {
      //     icon: 2,
      //     btnAlign: 'c',
      //     shade: false,
      //     end:function(){
      //       vm.ArrData[index].amount ='100,000';
      //     }
      //   });
      //   return;
      // }
      this.ArrData[index].proportion = (am / this.total) * 100 + '%';
      this.setPrice();
    },
    setPrice: function () {
      var syl = 0;
      var yp = 0;
      this.ArrData.forEach(function (item, index) {
        syl += Number(item.proportion.replace("%", '')) * Number(item.price.replace("%", '')) * 0.0001;
        yp += Number(item.amount.replace(/,/g, ''));
      });
      this.syl_txt = "组合收益率：" + (syl * 100).toFixed(2) + "%";
      this.spzje_txt = (this.total-yp).toLocaleString();
    },
    addItem: function () {
      this.ArrData.push({
        AssetClass: '0',
        amount: '',
        proportion: '',
        price: '',
        mark: '',
        clasN:'请选择'
      });
    },
    saveForm:function () {
      console.log(this.ArrData)
      var pri = Number(this.spzje_txt.replace(/,/g, ''));
      console.log(this.spzje_txt)
      if(pri==0){
        layer.alert('操作成功！', {
          icon: 1,
          btnAlign: 'c',
          shade: false
        })
      }
      if(pri<0){
        layer.alert('资产配置金额超过待投资总金额，<br/>请修改后再次保存！', {
          icon: 2,
          btnAlign: 'c',
          shade: false
        })
      }
      if(pri>0){
        layer.alert('尚有待投资金额未配置完，请继续配置！', {
          icon: 0,
          btnAlign: 'c',
          shade: false
        })
      }
    },
    setData:function(index,data){
      this.ArrData[index].clasN = data;
    }
  }
});